// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   IsoType.java

package com.solab.iso8583;

import java.io.PrintStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public final class IsoType
{

	public static final IsoType NUMERIC = new IsoType(true, 0);
	public static final IsoType ALPHA = new IsoType(true, 0);
	public static final IsoType LLVAR = new IsoType(false, 0);
	public static final IsoType LLLVAR = new IsoType(false, 0);
	public static final IsoType LLLVARCN = new IsoType(false, 0);
	public static final IsoType DATE14 = new IsoType(false, 14);
	public static final IsoType DATE10 = new IsoType(false, 10);
	public static final IsoType DATE8 = new IsoType(false, 8);
	public static final IsoType DATE4 = new IsoType(false, 4);
	public static final IsoType DATE_EXP = new IsoType(false, 4);
	public static final IsoType TIME = new IsoType(false, 6);
	public static final IsoType AMOUNT = new IsoType(false, 12);
	private boolean needsLen;
	private int length;

	private IsoType(boolean flag, int l)
	{
		needsLen = flag;
		length = l;
	}

	public boolean needsLength()
	{
		return needsLen;
	}

	public int getLength()
	{
		return length;
	}

	public String format(Date value)
	{
		if (this == DATE14)
			return (new SimpleDateFormat("YYYYMMddHHmmss")).format(value);
		if (this == DATE10)
			return (new SimpleDateFormat("MMddHHmmss")).format(value);
		if (this == DATE8)
			return (new SimpleDateFormat("MMddHHmmss")).format(value);
		if (this == DATE4)
			return (new SimpleDateFormat("MMdd")).format(value);
		if (this == DATE_EXP)
			return (new SimpleDateFormat("yyMM")).format(value);
		if (this == TIME)
			return (new SimpleDateFormat("HHmmss")).format(value);
		else
			throw new IllegalArgumentException((new StringBuilder("Cannot format date as ")).append(this).toString());
	}

	public String format(String value, int length)
	{
		if (this == ALPHA)
		{
			if (value == null)
				value = "";
			if (value.length() > length)
				return value.substring(0, length);
			char c[] = new char[length];
			System.arraycopy(value.toCharArray(), 0, c, 0, value.length());
			for (int i = value.length(); i < c.length; i++)
				c[i] = ' ';

			return new String(c);
		}
		if (this == LLVAR || this == LLLVAR)
			return value;
		if (this == LLLVARCN)
		{
			System.out.println("get in LLLVARCN ");
			return value;
		}
		if (this == NUMERIC)
		{
			char c[] = new char[length];
			char x[] = value.toCharArray();
			if (x.length > length)
				throw new IllegalArgumentException((new StringBuilder("Numeric value is larger than intended length: ")).append(value).append(" LEN ").append(length).toString());
			int lim = c.length - x.length;
			for (int i = 0; i < lim; i++)
				c[i] = '0';

			System.arraycopy(x, 0, c, lim, x.length);
			return new String(c);
		} else
		{
			throw new IllegalArgumentException((new StringBuilder("Cannot format String as ")).append(this).toString());
		}
	}

	public String format(long value, int length)
	{
		if (this == NUMERIC)
		{
			char c[] = new char[length];
			char x[] = Long.toString(value).toCharArray();
			if (x.length > length)
				throw new IllegalArgumentException((new StringBuilder("Numeric value is larger than intended length: ")).append(value).append(" LEN ").append(length).toString());
			int lim = c.length - x.length;
			for (int i = 0; i < lim; i++)
				c[i] = '0';

			System.arraycopy(x, 0, c, lim, x.length);
			return new String(c);
		}
		if (this == ALPHA || this == LLVAR || this == LLLVAR)
			return format(Long.toString(value), length);
		if (this == AMOUNT)
		{
			String v = Long.toString(value);
			char digits[] = new char[12];
			for (int i = 0; i < 12; i++)
				digits[i] = '0';

			System.arraycopy(v.toCharArray(), 0, digits, 10 - v.length(), v.length());
			return new String(digits);
		} else
		{
			throw new IllegalArgumentException((new StringBuilder("Cannot format number as ")).append(this).toString());
		}
	}

	public String format(BigDecimal value, int length)
	{
		if (this == AMOUNT)
		{
			String v = (new DecimalFormat("0000000000.00")).format(value);
			return (new StringBuilder(String.valueOf(v.substring(0, 10)))).append(v.substring(11)).toString();
		}
		if (this == NUMERIC)
			return format(value.longValue(), length);
		if (this == ALPHA || this == LLVAR || this == LLLVAR)
			return format(value.toString(), length);
		else
			throw new IllegalArgumentException((new StringBuilder("Cannot format BigDecimal as ")).append(this).toString());
	}

	public static IsoType valueOf(String s)
	{
		if (s.equals("NUMERIC"))
			return NUMERIC;
		if (s.equals("ALPHA"))
			return ALPHA;
		if (s.equals("LLVAR"))
			return LLVAR;
		if (s.equals("LLLVAR"))
			return LLLVAR;
		if (s.equals("LLLVARCN"))
			return LLLVARCN;
		if (s.equals("DATE14"))
			return DATE14;
		if (s.equals("DATE8"))
			return DATE8;
		if (s.equals("DATE10"))
			return DATE10;
		if (s.equals("DATE4"))
			return DATE4;
		if (s.equals("DATE_EXP"))
			return DATE_EXP;
		if (s.equals("TIME"))
			return TIME;
		if (s.equals("AMOUNT"))
			return AMOUNT;
		else
			throw new IllegalArgumentException((new StringBuilder("Unexistent IsoType ")).append(s).toString());
	}

}
